/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : RhymingWordFamilyDisplayAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 1, 2008       Raghu Sarraff			       Created
 */
package lwf.web.struts.action.english;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.factory.PropertyReader;
import lwf.handler.IProcessHandler;
import lwf.handler.english.RhymingWordFamilyHandler;
import lwf.web.struts.action.BaseAction;
import lwf.web.struts.action.form.english.RhymingWordFamilyActionForm;

import net.lwf.web.constant.IPageTitle;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


/**
 * Type the class description here
 *  
 */
//public class RhymingWordFamilyDisplayAction extends DispatchAction {
public class RhymingWordFamilyDisplayAction extends BaseAction {
	
	public ActionForward home(ActionMapping mapping, 
			                  ActionForm form,
							  HttpServletRequest request, 
							  HttpServletResponse response) {
		RhymingWordFamilyActionForm rwform = (RhymingWordFamilyActionForm) form;
		rwform.setRhymingwords(this.getRWFamilies());
		return mapping.findForward("home");
	} //home

	public ActionForward display(ActionMapping mapping,
			                     ActionForm form, 
								 HttpServletRequest request,
								 HttpServletResponse response) throws Exception {
		String wordfamily = request.getParameter("wordfamily");
        RhymingWordFamilyHandler rwfh = new RhymingWordFamilyHandler(wordfamily);
		// Write the HTML to response
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(rwfh.getDisplayTable());
		//the html string value will be retrieved as 'req.responseText'
		// and will be displayed using the element id = questionblock
		out.flush();
		return null; // Not forwarding to anywhere, response is fully-cooked
	}
	
	private List getRWFamilies() {
		List lstRWFs = new ArrayList();
		lstRWFs.add(" ");
		Map mapRWFs = (Map) PropertyReader.getInstance().getRhymingWords();
		Iterator iter = mapRWFs.entrySet().iterator();
		while(iter.hasNext()) {
			Map.Entry mapentry = (Map.Entry) iter.next();
			lstRWFs.add(mapentry.getKey());
		}
		return lstRWFs;
	}

	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		return null;
	}

	@Override
	public String getPageTitle() {
		return IPageTitle.RYMING_WORDS;
	}

	@Override
	public String getMainTitle() {
		return null;
	}

	@Override
	public String getPdfLink() {
		return null;
	}

	@Override
	public String getQuizLink() {
		return null;
	}
	
} //RhymingWordFamilyDisplayAction

